$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/build/list',
        datatype: "json",
        colModel: [			
			{ label: '楼号ID', name: 'buildId', index: "build_id", width: 45, key: true ,hidden:true},
			{ label: '楼号', name: 'buildName', width: 75 },
//            { label: '所属部门', name: 'deptName', width: 75 },
//			{ label: '邮箱', name: 'email', width: 90 },
//			{ label: '手机号', name: 'mobile', width: 80 },
//			{ label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
//				return value === 0 ? 
//					'<span class="label label-danger">禁用</span>' : 
//					'<span class="label label-success">正常</span>';
//			}},
			{ label: '创建人', name: 'createUserName',  width: 90},
			{ label: '创建时间', name: 'createTime', index: "create_time", width: 90}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			buildName: null
		},
		showList: true,
		title:null,
//		roleList:{},
		build:{
//			status:1,
			buildId:null,
            buildName:null,
//			roleIdList:[]
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
//			vm.roleList = {};
//			vm.user = {deptName:null, deptId:null, status:1, roleIdList:[]};
			
			//获取角色信息
//			this.getRoleList();

//			vm.getDept();
		},
//        getDept: function(){
//            //加载部门树
//            $.get(baseURL + "sys/dept/list", function(r){
//                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
//                var node = ztree.getNodeByParam("deptId", vm.user.deptId);
//                if(node != null){
//                    ztree.selectNode(node);
//
//                    vm.user.deptName = node.name;
//				}
//            })
//        },
		update: function () {
			var buildId = getSelectedRow();
			if(buildId == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
			
			vm.getBuild(buildId);
			//获取角色信息
//			this.getRoleList();
		},
		getBuild: function(buildId){
			$.get(baseURL + "sys/build/info/"+buildId, function(r){
				vm.build = r.build;
			});
		},
		del: function () {
			var buildIds = getSelectedRows();
			if(buildIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/build/delete",
                    contentType: "application/json",
				    data: JSON.stringify(buildIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.build.buildId == null ? "sys/build/save" : "sys/build/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.build),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},

		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'buildName': vm.q.buildName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});